<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 2018/4/17
 * Time: 下午5:18
 */

namespace app\controller;




class Upload extends CommonController
{
    function upload_img()
    {

        $upload_path = UPLOAD_PATH."tmp/";
        try{
            if(empty($_FILES["file"]))
                exception("请上传文件");
            $file = $_FILES["file"];
            if($file["error"] !=0)
                exception("图片上传错误");
            if(!is_uploaded_file($file['tmp_name']))
                exception("图片上传错误1");
            $ext = substr($file['type'],6);
            if(!in_array($ext,['jpg','jpeg','gif','png']))
                exception("图片类型错误");
            $file_name = "/tmp/".NOW_TIME."-".md5($file['name']);
            if(!@move_uploaded_file($file['tmp_name'],UPLOAD_PATH.$file_name)){
                exception("上传图片错误");
            }

            $this->resultdata["data"] = [
                "path" => $file_name,
                "url"  => "/upload/show/path/".str_replace("/","@",$file_name)
            ];
            $this->resultdata["code"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }
        $this->ajaxResult();
    }
    function show()
    {
        $path = input("path","","trim");
        if(empty($path))
            exit();
        $path = UPLOAD_PATH.str_replace("@","/",$path);
        if(!is_file($path))
            exit();
        $imginfo= getimagesize($path);
        $type= end($imginfo);
        header("Content-Type:".$type);
        echo @readfile($path);
        exit();

    }
}